package com.wwm.petadopt;


import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.nostra13.universalimageloader.cache.disc.naming.HashCodeFileNameGenerator;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;

/**
 * ADAPTER
 */

public class SampleAdapter extends ArrayAdapter<PetDetail> {

    private static final String TAG = "SampleAdapter";

    static class ViewHolder {
        ImageView img_pet;
        TextView text_firstline;
        TextView text_secondline;
    }

    private final LayoutInflater mLayoutInflater;

    public SampleAdapter(final Context context, final int textViewResourceId) {
        super(context, textViewResourceId);
        mLayoutInflater = LayoutInflater.from(context);
    }

    @Override
    public View getView(final int position, View convertView, final ViewGroup parent) {

        ViewHolder vh;
        if (convertView == null) {
            convertView = mLayoutInflater.inflate(R.layout.list_item_sample, parent, false);
            vh = new ViewHolder();
            vh.text_firstline = (TextView) convertView.findViewById(R.id.textview_firstline);
            vh.text_secondline = (TextView) convertView.findViewById(R.id.textview_secondline);
            vh.img_pet = (ImageView) convertView.findViewById(R.id.image_info);

            convertView.setTag(vh);
        } else {
            vh = (ViewHolder) convertView.getTag();
        }

        //convertView.setBackgroundResource(mBackgroundColors.get(backgroundIndex));
        ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(convertView.getContext())
                // You can pass your own memory cache implementation
                .discCacheFileNameGenerator(new HashCodeFileNameGenerator())
                .build();

        DisplayImageOptions options = new DisplayImageOptions.Builder()
                .displayer(new RoundedBitmapDisplayer(30)) //rounded corner bitmap
                .cacheInMemory(true)
                .cacheOnDisc(true)
                .build();

        ImageLoader imageLoader = ImageLoader.getInstance();
        imageLoader.init(config);
        //imageLoader.displayImage("http://pic1.ooopic.com/uploadfilepic/sheji/2009-08-05/OOOPIC_SHIJUNHONG_20090805326b1a80e6c6aa6a.jpg", vh.img_pet, options);
        imageLoader.displayImage(getItem(position).imageUrl, vh.img_pet, options);
        vh.text_firstline.setText(getItem(position).petName);
        vh.text_secondline.setText(getItem(position).petVariety);
        return convertView;
    }

}